import * as _ from 'lodash';

let util = {};

util.title = function(title) {
    title = title || 'peas admin';
    window.document.title = title;
};

util.oneOf = function(ele, targetArr) {
    if (targetArr.indexOf(ele) >= 0) {
        return true;
    } else {
        return false;
    }
};

util.cloneRemoveListen = function(obj) {
    return JSON.parse(JSON.stringify(obj));
};

util.setSessionStorage = function(name, value) {
    try {
        sessionStorage.setItem(name, value);
    } catch (e) {
        console.log('sessionStorage cache to limit!');
    }
};

util.getSessionStorage = function(name) {
    return sessionStorage.getItem(name);
};

util.removeSessionStoreage = function(key) {
    sessionStorage.removeItem(key);
};

util.getRootDomain = function() {
    var host = window.location.hostname;
    return host.split('.').slice(-2).join('.');
};

util.getLocalStorage = function(name) {
    return localStorage.getItem(name);
};

util.setLocalStorage = function(name, value) {
    try {
        localStorage.setItem(name, value);
    } catch (e) {
        console.log('localStorage cache to limit!');
    }
};

util.getObjSessionStorage = function(key) {
    return JSON.parse(sessionStorage.getItem(key));
};

util.getObjLocalStorage = function(key) {
    return JSON.parse(localStorage.getItem(key));
};

util.queryString = function(item) {
    var svalue = location.search.match(new RegExp('[\?\&]' + item + '=([^\&]*)(\&?)', 'i'));
    return svalue ? svalue[1] : svalue;
};

export default util;
